//
//  SearchArticleTableview.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/4/29.
//  Copyright © 2016年 game. All rights reserved.
//

#import "SearchArticleTableview.h"
#import "NewsCell.h"
#import "ShoolDataModel.h"
#import "AFHTTPSessionManager.h"
#import "PageInfoModel.h"
#import "MJRefresh.h"
@interface SearchArticleTableview ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) NSMutableArray *listAry;
@property (nonatomic,assign) CGFloat cellHight;
@property (nonatomic,strong) ShoolDataModel  *model;

@property (nonatomic,strong)  PageInfoModel *infoModel;
@property (nonatomic,assign) NSInteger count;
@end

@implementation SearchArticleTableview

-(NSMutableArray *)listAry{
    if (!_listAry) {
        _listAry = [NSMutableArray array];
        
    }
    return _listAry;
}

-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    if (self = [super initWithFrame:frame style:style]) {
        self.dataSource = self;
        self.delegate = self;
        [self registerClass:[NewsCell class] forCellReuseIdentifier:@"cell"];
        
        self.footer  = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getMoreDataa)];
        
        self.count = 1;
        
        __weak typeof(self) weakSelf = self;
        self.TapBlok = ^{
            
            [weakSelf setSeachDic:weakSelf.seachDic];
        };
    }
    return self;
}

-(void)getDateWhithPage:(NSInteger)page Whithdic:(NSDictionary *)dic{
    
    NSString *url = [NSString stringWithFormat:@"http://school.diaoyur.cn/article/search?page=%ld",(long)page];
    
    [[Manager sharedManager]RequestWhithURL:url AndParameter:dic AndCompletionType:^(id responseObject, DIEError *error) {
        
        
        if (!error) {
            if ([responseObject[@"status"] isEqualToString:@"SUCCESS"]) {
                self.model  = [[ShoolDataModel alloc]initWithDictionary:responseObject[@"data"] error:nil];
                
                self.infoModel = self.model.page_info;
                [self.listAry addObjectsFromArray:self.model.article_list];
                [self.footer endRefreshing];
                self.isShowImage = YES;
                [self reloadData];
            }else{
                [self.footer endRefreshing];
                self.isShowImage = YES;
                [self reloadData];
            }
        }else{
        
            [self.footer endRefreshing];
               self.isShowFile = YES;
            [self reloadData];
        }
    }];
 
}

#pragma mark--上拉加载
- (void)getMoreDataa{
    
    if (self.footer.isRefreshing) {
        
    self.count+=1;
    
    if (self.count>self.infoModel.pageCount.integerValue) {
        
        [self.footer noticeNoMoreData];
    }else{
        
        [self getDateWhithPage:self.count Whithdic:self.seachDic];
    }
    }
}


-(void)setSeachDic:(NSDictionary *)seachDic{

    _seachDic = seachDic;
    [self.listAry removeAllObjects];
    [self getDateWhithPage:1 Whithdic:_seachDic];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.listAry.count;
}


-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return self.cellHight;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        cell.model =_listAry[indexPath.row];
        self.cellHight = cell.cellHight;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (_listAry.count!=0) {
        NEWAticListModel  *Model3 = _listAry[indexPath.row];

        if ([self.mainDelget respondsToSelector:@selector(ArticleClickWhithId:AndSkin:)]) {
            [self.mainDelget ArticleClickWhithId:Model3.AtcID AndSkin:Model3.skin.integerValue];
        }
        
    }
    
}

@end